#include "TaskQueue.h"


TaskQueue::TaskQueue(size_t capacity):m_capacity(capacity){}

TaskQueue::~TaskQueue(){

}

void TaskQueue::push(ElemType &&value){
    unique_lock<mutex> ul(m_mutex);
    while(full()){
        m_notFull.wait(ul);
    }
    m_que.push(std::move(value));
    m_notEmpty.notify_one();
}

ElemType TaskQueue::pop(){
    unique_lock<mutex> ul(m_mutex);
    while(empty()){
        m_notEmpty.wait(ul);
    }
    if(m_flag){
        ElemType temp = m_que.front();
        m_que.pop();
        m_notEmpty.notify_one();
        return temp;
    }else{
        return nullptr;
    }
    
}

bool TaskQueue::empty(){
    return m_que.size() == 0;
}

bool TaskQueue::full(){
    return m_que.size() == m_capacity;
}

void TaskQueue::wakeup(){
    m_flag = false;
    m_notEmpty.notify_all();
}